/**
 * @file
 * @author
 * @date
 * @brief
 */
#include "Motor.h"
#include "app_trace.h"
#include "app_util_platform.h"
#include "board_cfg.h"
#include "display_ctrl.h"
#include "display_drv.h"
#include "lcd_extra.h"
#include "nrf_delay.h"
#include "nrf_drv_common.h"
#include "nrf_gpio.h"
#include "nrf_spi.h"
#include "nrf_spim.h"
#define LOG(fmt, ...) app_trace_log(fmt "\r\n", ##__VA_ARGS__)

/*********************************************************************************************
 * @brief
 * @param [in] param1
 * @param [out] param2
 * @return ret
 * @note
 * @warning
 ********************************************************************************************/
void lcd_set_xy_without_data(uint16_t Xstart, uint16_t Xend, uint16_t Ystart, uint16_t Yend)
{

    uint8_t buffer[4] = {Xstart >> 8, Xstart & 0xff, Xend >> 8, Xend & 0xff};
    oled_write_cmd(0x2a);
    oled_write_data_bytes(buffer, 4);

    oled_write_cmd(0x2b);
    buffer[0] = Ystart >> 8;
    buffer[1] = Ystart & 0xff;
    buffer[2] = Yend >> 8;
    buffer[3] = Yend & 0xff;
    oled_write_data_bytes(buffer, 4);
}

void lcd_set_xy(uint16_t Xstart, uint16_t Xend, uint16_t Ystart, uint16_t Yend)
{
    lcd_set_xy_without_data(Xstart, Xend, Ystart, Yend);
    oled_write_cmd(0x2c);
}

/*********************************************************************************************
 * @brief 填入数据
* @warning 图片w/h大小都不能超过255
                    注意！！p_data会自动根据OLED_SIZE_X跳跃
 ********************************************************************************************/
void oled_fill_data(uint16_t x, uint16_t y, uint16_t w, uint16_t h, const uint8_t *p_data)
{
    lcd_set_xy(x, x + w - 1, y, y + h - 1);
    if (get_spi_mode() != SPI_CFG_MODE_SOFTWARE)
    {
        spi_switch_mode(SPI_CFG_MODE_HARDWARE_DMA);
    }
    nrf_gpio_pin_clear(OLED_CS_PIN);
    nrf_gpio_pin_set(OLED_DC_PIN);

    uint16_t *color = (uint16_t *)p_data;
    uint16_t cnt = w * h;
    if (get_spi_mode() == SPI_CFG_MODE_SOFTWARE)
    {
        for (uint16_t i = 0; i < h; i++)
        {
            for (uint16_t j = 0; j < w * 2; j += 2)
            {
                oled_spi_tx_byte(p_data[j + 1]);
                oled_spi_tx_byte(p_data[j]);
            }
            p_data += w * 2; // 2字节
        }
    }
    else if (get_spi_mode() == SPI_CFG_MODE_HARDWARE_DMA)
    {
        for (uint16_t i = 0; i < h; i++)
        {
            for (uint16_t j = 0; j < w * 2; j += 2)
            {
                *color = __REV16(*color);
                color++;
            }
        }
        spim_tx(p_data, w, h * 2 - 1);
        // spim_wait_finish();
    }
    else
    {
        oled_spi_tx_bytes_hw(p_data, cnt * 2);
        oled_spi_hw_wait_end();
        nrf_gpio_pin_set(OLED_CS_PIN);
    }
}

/*********************************************************************************************
 * @brief 填入数据
* @warning 图片w/h大小都不能超过255
                    注意！！p_data会自动根据OLED_SIZE_X跳跃
 ********************************************************************************************/
void oled_fill_data_continue(uint16_t w, uint16_t h, const uint8_t *p_data)
{
    if (get_spi_mode() != SPI_CFG_MODE_SOFTWARE)
    {
        spi_switch_mode(SPI_CFG_MODE_HARDWARE_DMA);
    }
    nrf_gpio_pin_clear(OLED_CS_PIN);
    nrf_gpio_pin_set(OLED_DC_PIN);
    uint16_t cnt = w * h;
    uint16_t *color = (uint16_t *)p_data;

    if (get_spi_mode() == SPI_CFG_MODE_SOFTWARE)
    {
        for (uint16_t i = 0; i < h; i++)
        {
            for (uint16_t j = 0; j < w * 2; j += 2)
            {
                oled_spi_tx_byte(p_data[j + 1]);
                oled_spi_tx_byte(p_data[j]);
            }
            p_data += w * 2; // 2字节
        }
    }
    else if (get_spi_mode() == SPI_CFG_MODE_HARDWARE_DMA)
    {
        for (uint16_t i = 0; i < h; i++)
        {
            for (uint16_t j = 0; j < w * 2; j += 2)
            {
                *color = __REV16(*color);
                color++;
            }
        }
        spim_tx(p_data, w, h * 2 - 1);
    }
    else
    {
        oled_spi_tx_bytes_hw(p_data, cnt * 2);
        oled_spi_hw_wait_end();
        nrf_gpio_pin_set(OLED_CS_PIN);
    }
}
/*********************************************************************************************
 * @brief 全屏填入颜色
 * @warning
 ********************************************************************************************/
void oled_all_fill_data(uint16_t data)
{
    lcd_set_xy(0, OLED_SIZE_X - 1, 0, OLED_SIZE_Y - 1);

    nrf_gpio_pin_clear(OLED_CS_PIN);
    nrf_gpio_pin_set(OLED_DC_PIN);
    uint8_t buffer[2] = {data >> 8, data & 0xff};
    if (get_spi_mode() == SPI_CFG_MODE_SOFTWARE)
    {
        for (uint16_t i = 0; i < OLED_SIZE_X * OLED_SIZE_Y; i++)
        {
            oled_spi_tx_byte(buffer[0]);
            oled_spi_tx_byte(buffer[1]);
        }
    }
    else
    {
        for (uint16_t i = 0; i < OLED_SIZE_X * OLED_SIZE_Y; i++)
        {
            oled_spi_tx_bytes_hw(buffer, 2);
        }
    }

    oled_spi_hw_wait_end();
    nrf_gpio_pin_set(OLED_CS_PIN);
}

/*********************************************************************************************
 * @brief 清屏并恢复RAM映射
 * @warning
 ********************************************************************************************/
void oled_reset_clear(void)
{
    lcd_set_ram_offset(0);
    lcd_set_xy(0, OLED_SIZE_X - 1, 0, OLED_SIZE_Y - 1);

    nrf_gpio_pin_clear(OLED_CS_PIN);
    nrf_gpio_pin_set(OLED_DC_PIN);
    uint8_t buffer[2] = {0 >> 8, 0 & 0xff};
    for (uint32_t i = 0; i < OLED_SIZE_X * OLED_RAM_Y_MAX; i++)
    {
        oled_spi_tx_bytes_hw(buffer, 2);
    }
    oled_spi_hw_wait_end();
    nrf_gpio_pin_set(OLED_CS_PIN);
}

// #define LCD_RAM_DEFAULT_OFFSET 0

/*********************************************************************************************
 * @brief 设置RAM的偏移量
 * @param [in] param1
 * @param [out] param2
 * @return ret
 * @note
 * @warning RAM范围0~319，
 ********************************************************************************************/
void lcd_set_ram_offset(uint16_t offset)
{
    offset = (offset + LCD_RAM_DEFAULT_OFFSET) % 320;
#ifdef SCREEN_XY_MIRROR
    offset = 320 - offset;
#endif
    // LOG("%d", offset);
    oled_write_cmd(0x37);
    oled_write_data(offset >> 8);
    oled_write_data(offset & 0xFF);
}

void lcd_drv_set_param(void)
{
    oled_write_cmd(0x36);
#ifdef SCREEN_XY_MIRROR
    oled_write_data(0x88);
#else
    oled_write_data(0x48);
#endif

    oled_write_cmd(0x3a);
    oled_write_data(0x05);

    oled_write_cmd(0xC6); // frame_rate_control
    oled_write_data(0xef);

    oled_write_cmd(0xB0); //!< ram_control
    oled_write_data(0x00);
    oled_write_data(0xD8);

    lcd_extra_config();

    //oled_write_cmd(0x21);
    // lcd_set_ram_offset(0);
    lcd_set_ram_offset(disp_ctrl.last_y_offset);

    lcd_set_xy_without_data(0, OLED_SIZE_X - 1, 0, OLED_SIZE_Y - 1);
}
/*********************************************************************************************
 * @brief 显示驱动初始化
 * @param [in] param1
 * @param [out] param2
 * @return ret
 * @note
 * @warning
 ********************************************************************************************/
void lcd_drv_init(void)
{
    oled_spi_init(SPI_CFG_MODE_HARDWARE_DMA);
    nrf_gpio_cfg_output(OLED_DC_PIN);
#if USE_OLED_RESET

    nrf_gpio_pin_write(OLED_RESET_PIN, 1);
    nrf_delay_us(200);

    nrf_gpio_pin_write(OLED_RESET_PIN, 0);
    nrf_delay_us(800);

    nrf_gpio_pin_write(OLED_RESET_PIN, 1);
    nrf_delay_us(800);
#else
    oled_write_cmd(0x01);
    nrf_delay_ms(120);
#endif

    lcd_drv_set_param();
}
// void lcd_drv_init(void)
// {
//     oled_spi_init(SPI_CFG_MODE_HARDWARE_DMA);
//     nrf_gpio_cfg_output(OLED_DC_PIN);
// #if USE_OLED_RESET

//     nrf_gpio_pin_write(OLED_RESET_PIN, 1);
//     nrf_delay_us(200);

//     nrf_gpio_pin_write(OLED_RESET_PIN, 0);
//     nrf_delay_us(800);

//     nrf_gpio_pin_write(OLED_RESET_PIN, 1);
//     nrf_delay_us(800);
// #endif
// #if 1
//     oled_write_cmd(0xfe);
//     oled_write_cmd(0xef);

//     oled_write_cmd(0x36);
//     oled_write_data(0x48);
//     oled_write_cmd(0x3a);
//     oled_write_data(0x05);

//     oled_write_cmd(0x86);
//     oled_write_data(0x98);
//     oled_write_cmd(0x89);
//     oled_write_data(0x03);

//     oled_write_cmd(0x8b);
//     oled_write_data(0x80);

//     oled_write_cmd(0x8d);
//     oled_write_data(0x33);
//     oled_write_cmd(0x8e);
//     oled_write_data(0x8f);

//     // inversion
//     oled_write_cmd(0xe8);
//     oled_write_data(0x12);
//     oled_write_data(0x00);

//     oled_write_cmd(0xc3);
//     oled_write_data(0x20);

//     oled_write_cmd(0xc4);
//     oled_write_data(0x30);

//     oled_write_cmd(0xc9);
//     oled_write_data(0x08);

//     oled_write_cmd(0xff);
//     oled_write_data(0x62);

//     oled_write_cmd(0x99);
//     oled_write_data(0x3e);
//     oled_write_cmd(0x9d);
//     oled_write_data(0x4b);
//     oled_write_cmd(0x98);
//     oled_write_data(0x3e);
//     oled_write_cmd(0x9c);
//     oled_write_data(0x4b);

//     oled_write_cmd(0xf0);
//     oled_write_data(0x13);
//     oled_write_data(0x14);
//     oled_write_data(0x07);
//     oled_write_data(0x05);
//     oled_write_data(0xf0);
//     oled_write_data(0x29);

//     oled_write_cmd(0xf1);
//     oled_write_data(0x3e);
//     oled_write_data(0x92);
//     oled_write_data(0x90);
//     oled_write_data(0x21);
//     oled_write_data(0x23);
//     oled_write_data(0x9f);

//     oled_write_cmd(0xf2);
//     oled_write_data(0x13);
//     oled_write_data(0x14);
//     oled_write_data(0x07);
//     oled_write_data(0x05);
//     oled_write_data(0xf0);
//     oled_write_data(0x29);

//     oled_write_cmd(0xf3);
//     oled_write_data(0x3e);
//     oled_write_data(0x92);
//     oled_write_data(0x90);
//     oled_write_data(0x21);
//     oled_write_data(0x23);
//     oled_write_data(0x9f);

//     // oled_write_cmd(0x11);
//     // nrf_delay_ms(120);

//     // oled_write_cmd(0x29);
//     // oled_write_cmd(0x21);
//     lcd_set_ram_offset(0);
//     // lcd_set_xy(0, OLED_SIZE_X - 1, 0, OLED_SIZE_Y - 1);
// //   oled_all_fill_data(RGB565_COLOR_WHITE);
// //     oled_all_fill_data(RGB565_COLOR_BLACK);
// //     oled_all_fill_data(RGB565_COLOR_RED);
// //     oled_all_fill_data(RGB565_COLOR_GREEN);
// //     oled_all_fill_data(0x7f65);
// #else
//     //#define HSD_PARA
//     oled_write_cmd(0x01); // SLEEP OUT
//     nrf_delay_ms(120);

//     oled_write_cmd(0x11);
//     nrf_delay_ms(120);

//     oled_write_cmd(0x36);
// #ifdef SCREEN_XY_MIRROR
//     oled_write_data(0xc0);
// #else
//     oled_write_data(0x00);
// #endif

//     oled_write_cmd(0x3a); // SLEEP OUT
// #ifdef HSD_PARA
//     oled_write_data(0x55);
// #else
//     oled_write_data(0x05);
// #endif

//     oled_write_cmd(0xB2);
// //#ifdef HSD_PARA
// #if 1
//     oled_write_data(0x0C);
//     oled_write_data(0x0C);
//     oled_write_data(0x00);
//     oled_write_data(0x33);
//     oled_write_data(0x33);
// #else
//     oled_write_data(0x7F);
//     oled_write_data(0x7F);
//     oled_write_data(0x0);
//     oled_write_data(0xFF);
//     oled_write_data(0xFF);
// #endif

//     oled_write_cmd(0xB7);
//     oled_write_data(0x35);

//     oled_write_cmd(0xBB);
//     oled_write_data(0x23);

//     oled_write_cmd(0xC2);
//     oled_write_data(0x01);

//     oled_write_cmd(0xC3);
//     oled_write_data(0x00);

//     oled_write_cmd(0xC4);
//     oled_write_data(0x20);

//     oled_write_cmd(0xC6); // vertical scrolling start address
//     oled_write_data(0xef);

//     oled_write_cmd(0xD0);
//     oled_write_data(0xA4);
//     oled_write_data(0xA1);

//     oled_write_cmd(0xE0);
//     oled_write_data(0xD0);
//     oled_write_data(0x01);
//     oled_write_data(0x03);
//     oled_write_data(0x26);
//     oled_write_data(0x04);
//     oled_write_data(0x01);
//     oled_write_data(0x1E);
//     oled_write_data(0x33);
//     oled_write_data(0x36);
//     oled_write_data(0x17);
//     oled_write_data(0x33);
//     oled_write_data(0x14);
//     oled_write_data(0x24);
//     oled_write_data(0x2B);

//     oled_write_cmd(0xE1);
//     oled_write_data(0xD0);
//     oled_write_data(0x01);
//     oled_write_data(0x03);
//     oled_write_data(0x26);
//     oled_write_data(0x04);
//     oled_write_data(0x02);
//     oled_write_data(0x1D);
//     oled_write_data(0x33);
//     oled_write_data(0x37);
//     oled_write_data(0x18);
//     oled_write_data(0x34);
//     oled_write_data(0x14);
//     oled_write_data(0x23);
//     oled_write_data(0x2B);

//     oled_write_cmd(0xB0);
//     oled_write_data(0x00);
//     oled_write_data(0xD8);

//     oled_write_cmd(0x21);
//     lcd_set_ram_offset(0);
//     lcd_set_xy(0, OLED_SIZE_X - 1, 0, OLED_SIZE_Y - 1);
// #endif
//     // oled_turn_off();
// }
/*********************************************************************************************
 * @brief 关闭显示
 * @warning
 ********************************************************************************************/
void oled_turn_off(void)
{
    LOG("oled_turn_off");
    oled_write_cmd(0x28);
    oled_write_cmd(0x10); // sleep in
    nrf_delay_ms(120);
}
/*********************************************************************************************
 * @brief 打开显示
 * @warning
 ********************************************************************************************/

void oled_turn_on(void)
{
    //  peripheral_oledpower_On();

    //    #warning debug
    //    lcd_drv_init();
    //    lcd_set_ram_offset(disp_ctrl.last_y_offset);

    lcd_drv_set_param();
    oled_write_cmd(0x29);
    oled_write_cmd(0x11); // sleep out
    nrf_delay_ms(5);

    // oled_write_cmd(0x2c);
    // oled_all_fill_data(RGB565_COLOR_WHITE);
    // oled_all_fill_data(RGB565_COLOR_BLACK);
}
